package com.aspose.words.maven.examples;

import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.w3c.dom.NodeList;
import com.aspose.words.maven.utils.AsposeConstants;
import com.aspose.words.maven.utils.AsposeMavenProjectManager;
import java.io.File;
import java.io.IOException;

public class AsposeExampleSupport {

	final private static String localExampleFolder = "aspose/GitConsRepos/Aspose.Words/Examples";
	final private static String localExampleSourceFolder = "src/main/java/com/aspose/words/examples";
	final private static String localExampleResourceFolder = "src/main/resources/com/aspose/words/examples";

	public static void createExample(String selectedProjectPath, String exampleCategory, IProject project) {
		String srcExamplePath = System.getProperty("user.home") + File.separator + localExampleFolder + File.separator
				+ localExampleSourceFolder;
		String srcExampleResourcePath = System.getProperty("user.home") + File.separator + localExampleFolder
				+ File.separator + localExampleResourceFolder;

		String destProjectExamplePath = selectedProjectPath + File.separator + localExampleSourceFolder;
		String destProjectExampleResourcePath = selectedProjectPath + File.separator + localExampleResourceFolder;

		File srcExampleCategoryPath = new File(srcExamplePath + File.separator + exampleCategory);
		File destExampleCategoryPath = new File(destProjectExamplePath + File.separator + exampleCategory);

		Path srcUtil = new File(srcExamplePath + File.separator + "Utils.java").toPath();
		Path destUtil = new File(destProjectExamplePath + File.separator + "Utils.java").toPath();

		File srcExampleResourceCategoryPath = new File(srcExampleResourcePath + File.separator + exampleCategory);
		File destExampleResourceCategoryPath = new File(
				destProjectExampleResourcePath + File.separator + exampleCategory);

		String repositoryPOM_XML = System.getProperty("user.home") + File.separator + localExampleFolder
				+ File.separator + AsposeConstants.MAVEN_POM_XML;

		try {
			FileUtils.copyDirectory(srcExampleCategoryPath, destExampleCategoryPath);
			Files.copy(srcUtil, destUtil);
			FileUtils.copyDirectory(srcExampleResourceCategoryPath, destExampleResourceCategoryPath);

			NodeList examplesNoneAsposeDependencies = AsposeMavenProjectManager.getInstance()
					.getDependenciesFromPOM(repositoryPOM_XML, AsposeConstants.ASPOSE_GROUP_ID);
			AsposeMavenProjectManager.getInstance().addMavenDependenciesInProject(examplesNoneAsposeDependencies);

			NodeList examplesNoneAsposeRepositories = AsposeMavenProjectManager.getInstance()
					.getRepositoriesFromPOM(repositoryPOM_XML, AsposeConstants.ASPOSE_MAVEN_REPOSITORY);
			AsposeMavenProjectManager.getInstance().addMavenRepositoriesInProject(examplesNoneAsposeRepositories);

			project.refreshLocal(IResource.DEPTH_INFINITE, null);

		} catch (IOException | CoreException e) {
			e.printStackTrace();
		}

	}

}